package com.huawei.crm.web.controller;

import com.github.pagehelper.PageInfo;
import com.huawei.crm.domain.Employee;
import com.huawei.crm.query.EmployeeQueryObject;
import com.huawei.crm.query.IDDTO;
import com.huawei.crm.service.IDepartmentService;
import com.huawei.crm.service.IEmployeeService;
import com.huawei.crm.service.IRoleService;
import com.huawei.crm.util.JsonResult;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController extends BaseController{

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IRoleService roleService;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"员工列表", "employee:list"}, logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo) {
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        //查询高级查询表单中需要的部门信息
        model.addAttribute("depts", departmentService.listAll());
        return "employee/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"员工删除", "employee:delete"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
        try {
            if (id != null) {
                employeeService.delete(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("亲,批量删除失败!");
        }
        return result;
    }

    @RequestMapping("/batchDelete")
    @RequiresPermissions(value = {"员工批量删除", "employee:batchDelete"}, logical = Logical.OR)
    @ResponseBody
    public JsonResult batchDelete(Long[] ids) {
        JsonResult result = new JsonResult();
        try {
            if (ids != null) {
                employeeService.batchDelete(ids);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("亲,批量删除失败!");
        }
        return result;
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"员工保存或更改操作", "employee:saveOrUpdate"}, logical = Logical.OR)
    public String saveOrUpdate(Employee d, Long[] roleIds) {
        employeeService.saveOrUpdate(d, roleIds);
        return "redirect:/employee/list";
    }

    @RequestMapping("/input")
    @RequiresPermissions(value = {"员工编辑", "employee:input"}, logical = Logical.OR)
    public String input(Model model, Long id) {
        if (id != null) {
            Employee e = employeeService.get(id);
            model.addAttribute("e", e);
        }
        //查询高级查询表单中需要的部门信息
        model.addAttribute("depts", departmentService.listAll());

        //查询所有的角色信息
        model.addAttribute("roles", roleService.listAll());
        return "employee/input";
    }

    @RequestMapping("/exportXls")
    @RequiresPermissions(value = {"员工导出", "employee:exportXls"}, logical = Logical.OR)
    public void exportXls(HttpServletResponse response) throws IOException {
        //设置下载文件的名称
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        // 创建工作簿
        // 在工作簿中写数据
        Workbook book = employeeService.exportXls();
        // 将工作簿保存到磁盘中
        book.write(response.getOutputStream());
    }

    @RequiresPermissions(value = {"员工导入", "employee:improtXls"}, logical = Logical.OR)
    @RequestMapping("/importXls")
    public String improtXls(MultipartFile file) throws IOException {
        employeeService.improtXls(file.getInputStream());
        return "redirect:/employee/list";
    }

    @RequestMapping("/exportXlsx")
    @RequiresPermissions(value = {"员工导出", "employee:exportXls"}, logical = Logical.OR)
    public void exportXlsx(HttpServletResponse response) throws IOException {
       employeeService.exportXlsx(response);
    }

    @PostMapping("/batchExportZip")
    public void batchExportZip(HttpServletResponse response, @RequestBody IDDTO idDTO) throws Exception {

        List<File> files = employeeService.exportAllXls(response, idDTO);

        // 将多个文件打包成zip包
        downloadZipFile(files, "employeeInfo.zip" ,response);
    }


}
